package com.shang.controller;

import com.shang.service.ItemsESService;
import com.shang.utils.PagedGridResult;
import com.shang.utils.ShangJSONResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName ItemsController
 * @Description TODO
 * @Author Larry
 * @Date 2020/12/16 21:52
 * @Version 1.0
 */
@RestController
@RequestMapping("items")
public class ItemsController {

    @Autowired
    private ItemsESService itemsESService;

    @GetMapping("/hello")
    public Object hello(){
        return "elasticsearch";
    }

    @GetMapping("/es/search")
    public ShangJSONResult search(String keywords, String sort, Integer page, Integer pageSize) {

        if (StringUtils.isBlank(keywords)) {
            return ShangJSONResult.errorMsg(null);
        }

        if (page == null) {
            page = 1;
        }

        if (pageSize == null) {
            pageSize = 20;
        }

        page --;

        PagedGridResult grid = itemsESService.searhItems(keywords, sort, page, pageSize);

        return ShangJSONResult.ok(grid);
    }
}
